/*
 * YuebaoMagazine.cpp
 *
 *  Created on: 2011-3-13
 *      Author: wangchao
 */

#include "YuebaoMagazineBook.h"
#include "ProjectsDefines.h"
#include "fFile.h"
#include "fWml.h"
#include "BetterUserHBufC.h"
YuebaoMagazineCata::YuebaoMagazineCata( )
{
    
}

YuebaoMagazineCata::~YuebaoMagazineCata( )
{
    DebugLog("~");
    SAFE_DELETE( _name );
}

YuebaoMagazinePage::YuebaoMagazinePage( )
{
    
}

YuebaoMagazinePage::~YuebaoMagazinePage( )
{
    DebugLog("~");
    SAFE_DELETE( _name );
    SAFE_DELETE( _image );
    SAFE_DELETE( _text );
}

YuebaoMagazineBook::YuebaoMagazineBook( )
{
    _isValid = false;
}

YuebaoMagazineBook::~YuebaoMagazineBook( )
{
    DebugLog("~");
    _cates.ResetAndDestroy( );
    _pages.ResetAndDestroy( );
}

void YuebaoMagazineBook::ConstructL( )
{
    _isValid = false;
}
_LIT( kMagazineBoxNode, "box" );
_LIT( kMagazineListNode, "list");
_LIT( kMagazineItemNode, "item");
_LIT( kMagazineTapNode, "tap" );
_LIT( kMagazineNameNode, "name" );
_LIT( kMagazineLinkNode, "link" );
_LIT( kMagazineBrandNameNode, "brandname" );
_LIT( kMagazineIDNode, "id" );
_LIT( kMagazineImageNode, "image" );
_LIT( kMagazineTextNode, "text" );
_LIT( kMagazineContentNode, "content" );

void ParseContentNode( CElement* root, RPointerArray< YuebaoMagazinePage >& _pages )
{
    int count = root->ElementCount( );
    for( int index = 0; index < count; index++ )
    {
        CElement* element = root->GetElement( index );
        if( element )
        {
            YuebaoMagazinePage* page = new YuebaoMagazinePage( );
            
            page->_name = BetterUtility::GetElementText( element->GetElement( kMagazineBrandNameNode ) );
            page->_id = BetterUtility::GetElementTextAsInt( element->GetElement( kMagazineIDNode ) );
            page->_image = BetterUtility::GetNotNullElementText( element->GetElement( kMagazineImageNode ) );
            page->_text = BetterUtility::GetNotNullElementText( element->GetElement( kMagazineTextNode ) );
            
            _pages.Append( page );
        }
    }
}

void YuebaoMagazineBook::Load( const TDesC& path )
{
    HBufC *content = fFile::ReadTextToUnicode( path, fTextConverter::f_UTF8 );
    if( !content )
    {
        return;
    }

    CDomBuilder* xmlParse = CDomBuilder::NewL( );
    
    CElement* elementBox = xmlParse->ParseL( *content );
    
    if( elementBox && elementBox->GetName( ) == kMagazineBoxNode )
    {
        int count = elementBox->ElementCount( );
        for( int index = 0; index < count; index++ )
        {
            CElement* element = elementBox->GetElement( index );
            if( element )
            {
                if( element->GetName( ) == kMagazineListNode )
                {
                    int tapCount = element->ElementCount( );
                    for( int tapIndex = 0; tapIndex < tapCount; tapIndex++ )
                    {
                        CElement* elementTap = element->GetElement( tapIndex );
                        if( elementTap && elementTap->GetName( ) == kMagazineTapNode )
                        {
                            YuebaoMagazineCata* cate = new YuebaoMagazineCata( );
                            
                            cate->_name = BetterUtility::GetNotNullElementText( elementTap->GetElement( kMagazineNameNode ) );
                            cate->_link = BetterUtility::GetElementTextAsInt( elementTap->GetElement( kMagazineLinkNode ) );
                            
                            _cates.Append( cate );
                        }
                    }
                }
                else if( element->GetName( ) == kMagazineContentNode )
                {
                    ParseContentNode( element, _pages );
                }
                else if( element->GetName( ) == kMagazineItemNode )
                {
                    YuebaoMagazinePage* page = new YuebaoMagazinePage( );
                    
                    page->_name = BetterUtility::GetElementText( element->GetElement( kMagazineBrandNameNode ) );
                    page->_id = BetterUtility::GetElementTextAsInt( element->GetElement( kMagazineIDNode ) );
                    page->_image = BetterUtility::GetNotNullElementText( element->GetElement( kMagazineImageNode ) );
                    page->_text = BetterUtility::GetNotNullElementText( element->GetElement( kMagazineTextNode ) );
                    
                    _pages.Append( page );
                }
                else
                {
                    
                }
            }
        }
    }
    SAFE_DELETE( content );
    SAFE_DELETE( elementBox );
    SAFE_DELETE( xmlParse );
    _isValid = true;
}

bool YuebaoMagazineBook::IsValid( )
{
    if( _isValid )
    {
        if( _cates.Count( ) == 0 )
        {
            return false;
        }

        if( _pages.Count( ) == 0 )
        {
            return false;
        }
        
        return true;
        
    }
    
    return _isValid;
}

int YuebaoMagazineBook::PageIndex( int id )
{
    int count = _pages.Count( );
    
    for( int index = 0; index < count; index++ )
    {
        YuebaoMagazinePage* page = _pages.operator []( index );
        if( page && page->_id == id )
        {
            return index;
        }
    }
    
    return -1;
}
